package com.black.knight.chess.adapters;

import android.graphics.PorterDuff.Mode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.black.knight.chess.R;
import com.black.knight.chess.SahMatActivity;
import com.black.knight.chess.domain.Game;
import com.black.knight.chess.domain.Move;
import com.black.knight.chess.enums.ChessAction;
import com.black.knight.chess.enums.NewGame;
import com.black.knight.chess.model.SahModel;
import com.black.knight.chess.model.SettingsModel;

public class TableAdapter extends BaseAdapter {
    private SahMatActivity mContext;
    
    public static boolean animate = true;
    
	public Animation anim = null;

    public TableAdapter(SahMatActivity c) {
        mContext = c;
    }

    public int getCount() {
        return SahModel.getInstance().getCount();
    }

    public Integer getItem(int position) {
        return SahModel.getInstance().getItem(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    	if(SahModel.getInstance().isAutoRotate() && !SahModel.getInstance().getGame().getPlayer1().isWhitePlayer()) position = 63 - position;
    	
    	int multiple = 1;
    	
    	if(!NewGame.HUMAN.equals(SahModel.getInstance().getGame().getGameType()) && !SahModel.getInstance().getGame().getPlayer1().isWhitePlayer()) {
    		position = 63 - position;
    		multiple *= -1;
    	}
    	
    	final ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
        	imageView = (ImageView) LayoutInflater.from(mContext).inflate(R.layout.figure, parent, false);
        	
        	imageView.setLayoutParams(new AbsListView.LayoutParams(SahModel.getTableWidth() / 8, SahModel.getTableWidth() / 8));
        } else {
            imageView = (ImageView) convertView;
        }
        
        imageView.setImageResource(SahModel.getInstance().getItem(position));
                
        int flag = SahModel.getInstance().getSelectedFlags()[position];
        
        animate(imageView, flag, position, multiple);
        
        return imageView;
    }
    
    private void animate(final ImageView imageView, int flag, int position, int multiple) {
    	Game game = SahModel.getInstance().getGame();
    	
    	imageView.setColorFilter(null);
    	imageView.setAlpha(255);
    	
    	final ImageButton buttonPrevious = (ImageButton) mContext.findViewById(R.id.buttonPrevious);
        final ImageButton buttonNext = (ImageButton)  mContext.findViewById(R.id.buttonNext);
    	
    	if(flag == 1) {
        	if(SahModel.getInstance().getItem(position) != R.drawable.selector) {
        		imageView.setColorFilter(mContext.getResources().getColor(R.color.green), Mode.DST_ATOP);
        	} else {
        		imageView.setAlpha(150);
        	}
        } else if(flag == 2 || flag == 3) {
        	if(SahModel.getInstance().getItem(position) != R.drawable.empty) {  		        		
        		Move lastMove = null;
        		
        		if(ChessAction.PREVIOUS_MOVE.equals(SahModel.lastAction)) {
        			lastMove = game.getMoves().get(game.getSelectedIndex());
        		} else if(ChessAction.NEXT_MOVE.equals(SahModel.lastAction)){
        			lastMove = game.getMoves().get(game.getSelectedIndex() - 1);
        		} else if(ChessAction.MOVE.equals(SahModel.lastAction)){
        			lastMove = game.getMoves().get(game.getSelectedIndex() - 1);
        		}
        		
        		if(lastMove != null) {
	        		if(ChessAction.PREVIOUS_MOVE.equals(SahModel.lastAction)) {
	        			anim = new TranslateAnimation(multiple * (lastMove.getNewColumn() - lastMove.getColumn()) * 60, 0, multiple * (lastMove.getNewRow() - lastMove.getRow()) * 60, 0);
	        		} else {
	        			anim = new TranslateAnimation(multiple * (lastMove.getColumn() - lastMove.getNewColumn()) * 60, 0, multiple * (lastMove.getRow() - lastMove.getNewRow()) * 60, 0);
	        		}
        		}
        		
        		if(animate && anim != null && !anim.hasStarted() && SettingsModel.getInstance().isAnimateMove() && (flag == 2 || flag == 3 && lastMove.getDestroyedFigure() == R.drawable.empty || !ChessAction.PREVIOUS_MOVE.equals(SahModel.lastAction))) {	        		
	                anim.setDuration(400);
	                anim.setInterpolator(new AccelerateDecelerateInterpolator());
	                anim.setAnimationListener(new AnimationListener() {
						@Override
						public void onAnimationEnd(Animation animation) {
							if(SettingsModel.getInstance().isMarkLastMove()) {
								imageView.setColorFilter(mContext.getResources().getColor(R.color.green_yellow), Mode.DST_ATOP);
							}
			        		
			        		buttonNext.setEnabled(true);
							buttonPrevious.setEnabled(true);
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
						}

						@Override
						public void onAnimationStart(Animation animation) {
							imageView.setColorFilter(null);
				        	imageView.setAlpha(255);
				        	
							buttonNext.setEnabled(false);
							buttonPrevious.setEnabled(false);
						}
					});
	                imageView.setAnimation(anim);
	                anim.start();
        		} else {
        			if(SettingsModel.getInstance().isMarkLastMove()) {
        				imageView.setColorFilter(mContext.getResources().getColor(R.color.green_yellow), Mode.DST_ATOP);
        			}
        		}
        	} else {
        		if(SettingsModel.getInstance().isMarkLastMove()) {
	        		imageView.setImageResource(R.drawable.selector);
	        		imageView.setColorFilter(mContext.getResources().getColor(R.color.green_yellow), Mode.SRC_IN);
	        		imageView.setAlpha(200);
        		}
        	}
        } else if(flag == 4 || flag == 5) {
        	if(SahModel.getInstance().getItem(position) != R.drawable.empty) {
        		imageView.setColorFilter(mContext.getResources().getColor(R.color.orange), Mode.DST_ATOP);
        	} else {
        		imageView.setImageResource(R.drawable.selector);
        		imageView.setColorFilter(mContext.getResources().getColor(R.color.orange), Mode.SRC_IN);        		
        	}
        }
    }
}